package com.intexh.quickandroid.module.chat.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.intexh.quickandroid.R;
import com.intexh.quickandroid.utils.glide.GlideHelper;

import java.util.ArrayList;
import java.util.List;

import uk.co.senab.photoview.PhotoView;

public class ImageShowActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private TextView position_tv;
    private String[] urls;
    private int currentImagePosition;

    public static void startActivity(Context context,String[] urls,int currentImagePosition){
        Intent intent = new Intent(context,ImageShowActivity.class);
        intent.putExtra("urls",urls);
        intent.putExtra("currentImagePosition",currentImagePosition);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_show);
        urls = getIntent().getStringArrayExtra("urls");
        currentImagePosition = getIntent().getIntExtra("currentImagePosition",0);
        viewPager = (ViewPager)findViewById(R.id.viewPager);
        position_tv = (TextView)findViewById(R.id.position_tv);
        initViewPager();
    }

    private void initViewPager() {
        final List<ImageView> iamgeViews = new ArrayList<>();
        for(int i=0;i<urls.length;i++){
            PhotoView imageView = new PhotoView(this);
            GlideHelper.INSTANCE.loadImage(imageView,urls[i]);
            iamgeViews.add(imageView);
        }

        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return iamgeViews.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(iamgeViews.get(position));
                return iamgeViews.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                // 删除
                container.removeView(iamgeViews.get(position));
            }
        });
        position_tv.setText((currentImagePosition+1)+"/"+iamgeViews.size());
        viewPager.setCurrentItem(currentImagePosition);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                position_tv.setText((position+1)+"/"+iamgeViews.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
